package netTracker;

import java.io.Serializable;

/**
 * Indicates either the pitch of a note or a cutoff signal.
 * 
 * @author David Cummings
 */
public class Note implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	/** Define certain frequencies to represent types of notes. */
	public final static double EMPTY = 0;		// Indicates no note.
	public final static double CUTOFF = -1;	// Indicates a cutoff signal.
	
	/** The frequency of the note in Hz. 
	 * May also indicate one of the special signals listed above. */
	double frequency;
	/** A string representation of the note. */
	String noteName;
	
	/*
	 * Creates a Note.
	 * @param frequency the initial frequency of the note.
	 */
	public Note(double frequency){
		this.frequency = frequency;
		noteName = NoteMapper.lookup(frequency);
	}

	/*
	 * Creates a Note.
	 * @param frequency the initial frequency of the note.
	 */
	public Note(String noteName){
		this.noteName = noteName;
		frequency = NoteMapper.lookup(noteName);
	}
	
	public void setFrequency(Double frequency){
		this.frequency = frequency;
		noteName = NoteMapper.lookup(frequency);
	}
	
	/**
	 * Tries to set the note name of this note based on a string. 
	 * Does nothing if the string is not mapped.
	 * @param noteName the note name to set
	 */
	public void setNoteName(String noteName){
		Double result = NoteMapper.lookup(noteName);
		if(result != NoteMapper.FREQ_NOT_FOUND){
			this.noteName = noteName;
			frequency = result;
		}
	}
	
	/** @return a string representation of this note. */
	public String toString(){
		return noteName;
	}

	public double getFrequency() {
		return frequency;
	}
}
